﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using CarFrameWork.Common;
namespace CarFrameWork.Model
{
	/// <summary>
	/// 类NewList。
	/// </summary>
	[Serializable]
	public partial class NewList
	{
		public NewList()
		{}
		#region Model
		private int _id;
		private string _newtitle;
		private DateTime _newtime;
		private string _caption;
		/// <summary>
		/// 
		/// </summary>
		public int ID
		{
			set{ _id=value;}
			get{return _id;}
		}
		/// <summary>
		/// 
		/// </summary>
		public string NewTitle
		{
			set{ _newtitle=value;}
			get{return _newtitle;}
		}
		/// <summary>
		/// 
		/// </summary>
		public DateTime NewTime
		{
			set{ _newtime=value;}
			get{return _newtime;}
		}
		/// <summary>
		/// 
		/// </summary>
		public string Caption
		{
			set{ _caption=value;}
			get{return _caption;}
		}
		#endregion Model


		#region  Method

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public NewList(int ID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ID,NewTitle,NewTime,Caption ");
			strSql.Append(" FROM [NewList] ");
			strSql.Append(" where ID=@ID ");
			SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)};
			parameters[0].Value = ID;

			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["ID"]!=null && ds.Tables[0].Rows[0]["ID"].ToString()!="")
				{
					this.ID=int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
				}
				if(ds.Tables[0].Rows[0]["NewTitle"]!=null && ds.Tables[0].Rows[0]["NewTitle"].ToString()!="")
				{
					this.NewTitle=ds.Tables[0].Rows[0]["NewTitle"].ToString();
				}
				if(ds.Tables[0].Rows[0]["NewTime"]!=null && ds.Tables[0].Rows[0]["NewTime"].ToString()!="")
				{
					this.NewTime=DateTime.Parse(ds.Tables[0].Rows[0]["NewTime"].ToString());
				}
				if(ds.Tables[0].Rows[0]["Caption"]!=null && ds.Tables[0].Rows[0]["Caption"].ToString()!="")
				{
					this.Caption=ds.Tables[0].Rows[0]["Caption"].ToString();
				}
			}
		}

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{   
            return DbHelperSQL.GetMaxID("ID", "NewList"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int ID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from [NewList]");
			strSql.Append(" where ID=@ID ");

			SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)};
			parameters[0].Value = ID;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add()
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into [NewList] (");
			strSql.Append("NewTitle,NewTime,Caption)");
			strSql.Append(" values (");
			strSql.Append("@NewTitle,@NewTime,@Caption)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@NewTitle", SqlDbType.NVarChar,50),
					new SqlParameter("@NewTime", SqlDbType.DateTime),
					new SqlParameter("@Caption", SqlDbType.NVarChar)};
			parameters[0].Value = NewTitle;
			parameters[1].Value = NewTime;
			parameters[2].Value = Caption;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update()
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update [NewList] set ");
			strSql.Append("NewTitle=@NewTitle,");
			strSql.Append("NewTime=@NewTime,");
			strSql.Append("Caption=@Caption");
			strSql.Append(" where ID=@ID ");
			SqlParameter[] parameters = {
					new SqlParameter("@NewTitle", SqlDbType.NVarChar,50),
					new SqlParameter("@NewTime", SqlDbType.DateTime),
					new SqlParameter("@Caption", SqlDbType.NVarChar),
					new SqlParameter("@ID", SqlDbType.Int,4)};
			parameters[0].Value = NewTitle;
			parameters[1].Value = NewTime;
			parameters[2].Value = Caption;
			parameters[3].Value = ID;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int ID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from [NewList] ");
			strSql.Append(" where ID=@ID ");
			SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)};
			parameters[0].Value = ID;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public void GetModel(int ID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ID,NewTitle,NewTime,Caption ");
			strSql.Append(" FROM [NewList] ");
			strSql.Append(" where ID=@ID ");
			SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)};
			parameters[0].Value = ID;

			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["ID"]!=null && ds.Tables[0].Rows[0]["ID"].ToString()!="")
				{
					this.ID=int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
				}
				if(ds.Tables[0].Rows[0]["NewTitle"]!=null && ds.Tables[0].Rows[0]["NewTitle"].ToString()!="")
				{
					this.NewTitle=ds.Tables[0].Rows[0]["NewTitle"].ToString();
				}
				if(ds.Tables[0].Rows[0]["NewTime"]!=null && ds.Tables[0].Rows[0]["NewTime"].ToString()!="")
				{
					this.NewTime=DateTime.Parse(ds.Tables[0].Rows[0]["NewTime"].ToString());
				}
				if(ds.Tables[0].Rows[0]["Caption"]!=null && ds.Tables[0].Rows[0]["Caption"].ToString()!="")
				{
					this.Caption=ds.Tables[0].Rows[0]["Caption"].ToString();
				}
			}
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select * ");
			strSql.Append(" FROM [NewList] ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		#endregion  Method
	}
}

